package com.tycmc.signelectroniccontracts.tools.store;

import com.tycmc.signelectroniccontracts.tools.images.UrlPath;
import com.tycmc.signelectroniccontracts.tools.net.tools.Json;
import com.tycmc.signelectroniccontracts.tools.show.LogShow;


import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileStore {

    public static final int filesize = 950;

    /**
     * 保存文件
     *
     * @param fileName 文件的名字
     * @param bytes    字节流
     * @return
     */
    public static String save(String fileName, byte[] bytes) {
        File file = new File(UrlPath.FilePath + fileName);
        //如果不存在则新建
        // 创建FileOutputStream对象
        FileOutputStream outputStream = null;
        // 创建BufferedOutputStream对象
        BufferedOutputStream bufferedOutputStream = null;
        try {
            // 在文件系统中根据路径创建一个新的空文件
            if (file.exists()) {//如果存在则追加
            } else {
                file.createNewFile();
            }
            // 获取FileOutputStream对象
            outputStream = new FileOutputStream(file, true);
            // 获取BufferedOutputStream对象
            bufferedOutputStream = new BufferedOutputStream(outputStream);
            // 往文件所在的缓冲输出流中写byte数据
            bufferedOutputStream.write(bytes);
            // 刷出缓冲输出流，该步很关键，要是不执行flush()方法，那么文件的内容是空的。
            bufferedOutputStream.flush();
        } catch (Exception e) {
            // 打印异常信息
            e.printStackTrace();
        } finally {
            // 关闭创建的流对象
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }


        return UrlPath.FilePath + fileName;
    }



    private static byte[] doRead(InputStream is) throws IOException {
        DataInputStream dis = new DataInputStream(is);
        byte[] buffer = new byte[is.available()];
        dis.readFully(buffer);
        dis.close();
        is.close();
        return buffer;
    }

    //将字符串写入到文本文件中
    public static String writeTextToFile(String strcontent, String strFilePath) {

        //每次写入时都换行
        String strContent = strcontent + "\r\n";
        try {

            File file = new File(strFilePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            RandomAccessFile rf = new RandomAccessFile(file, "rwd");
            rf.seek(file.length());
            rf.write(strContent.getBytes());
            rf.close();
            return strFilePath;
        } catch (Exception e) {
            LogShow.e("TestFile", "Error on write File:" + e);
        }
        return "";
    }







    /**
     * 获取文件的总包数
     *
     * @param fileName 文件的路径
     * @param length   每包的数量 最后一包可能小于数量
     * @return
     */
    public static List<List<Byte>> getUpFile1(String fileName, int length) {
        File file = new File(fileName);
        try {
            InputStream in = new FileInputStream(file);
            byte[] bytes = doRead(in);
            List<List<Byte>> list = new ArrayList<>();
            List<Byte> byteList = new ArrayList<>();
            for (int i = 0; i < bytes.length; i++) {
                byte aByte = bytes[i];
                if (i % length == length - 1 || i == bytes.length - 1) {
                    if (byteList != null && byteList.size() != 0) {
                        byteList.add(aByte);
                        list.add(byteList);
                        byteList = new ArrayList<>();
                    } else {
                        byteList = new ArrayList<>();
                        byteList.add(aByte);
                    }

                } else {
                    if (byteList != null) {
                        byteList.add(aByte);
                    }
                }
            }
            return list;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<List<Byte>> getUpFile(String fileName, int length) {
        File file = new File(fileName);
        try {
            InputStream in = new FileInputStream(file);
            int len = 0;
            byte[] bytes1 = new byte[length];
            List<List<Byte>> list = new ArrayList<>();
            while ((len = in.read(bytes1)) != -1) {
                List<Byte> byteList = new ArrayList<>();
                for (int i = 0; i < len; i++) {
                    byteList.add(bytes1[i]);
                }
                list.add(byteList);
            }
            in.close();
//            byte[] bytes = doRead(in);
//
//            List<Byte> byteList = new ArrayList<>();
//            for (int i = 0; i < bytes.length; i++) {
//                byte aByte = bytes[i];
//                if (i % length == length - 1 || i == bytes.length - 1) {
//                    if (byteList != null && byteList.size() != 0) {
//                        byteList.add(aByte);
//                        list.add(byteList);
//                        byteList = new ArrayList<>();
//                    } else {
//                        byteList = new ArrayList<>();
//                        byteList.add(aByte);
//                    }
//
//                } else {
//                    if (byteList != null) {
//                        byteList.add(aByte);
//                    }
//                }
//            }
            return list;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取文件数据的流的长度
     *
     * @param fileName 文件数据的长度
     * @return
     */
    public static byte[] getFileSize(String fileName) {
        File file = new File(fileName);
        try {
            InputStream in = new FileInputStream(file);
            byte[] bytes = doRead(in);
            return bytes;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
